package com.hcss.common.shiro;

import com.hcss.login.service.LoginService;
import com.hcss.qywechat.user.model.vo.WQyUserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @描述: shiro登录认证
 * @作者: xuqian
 * @创建日期: 2018-06-25 16:21
 * @公司 浙江鸿程计算机系统有限公司
 */
@Slf4j
public class LoginRealm extends AuthorizingRealm {
    @Autowired
    private LoginService loginService;

    /**
     * 登录权限认证
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 登录身份认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 登录账号
        String userid = (String)authenticationToken.getPrincipal();
        log.debug(userid + "正在登陆。。。");
        WQyUserVO user = loginService.getUserByUserid(userid);
        if (user == null) {
            return null;
        }

        return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(""), user.getName());
    }

}
